package com.coo.net
{
	import com.coo.events.XMLLoaderEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLStream;

	public class CustomXMLLoader extends EventDispatcher
	{
		
		private var urlLoader: URLStream = new URLStream();
		public var encode: String = "euc-kr";
		
		public function CustomXMLLoader()
		{
			super();
		}
		
		public function load( url: String ): void
		{
			urlLoader.addEventListener( Event.COMPLETE, onComplete );
			urlLoader.addEventListener( IOErrorEvent.IO_ERROR, onIOError );
			
			urlLoader.load( new URLRequest( url ));
		}
		
		private function onIOError( e: IOErrorEvent ): void
		{
			trace( "Error - can not find file " );
		}
		
		private function onComplete( e: Event ): void
		{
			try
			{
				var xml:XML = new XML( urlLoader.readMultiByte( urlLoader.bytesAvailable, this.encode ) );
			}
			catch( e: TypeError )
			{
				trace( "TypeError - " +  e.errorID );
				return;
			}
			
			this.dispatchEvent( new XMLLoaderEvent( xml, XMLLoaderEvent.COMPLETE ));
		}
	}
}